package me.wangxuan.pat;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.HibernatePersistenceProvider;

import me.wangxuan.pat.entity.Account;
import me.wangxuan.pat.entity.Api;
import me.wangxuan.pat.entity.Project;

import javax.persistence.EntityManagerFactory;
import javax.persistence.SharedCacheMode;
import javax.persistence.ValidationMode;
import javax.persistence.spi.ClassTransformer;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.sql.DataSource;

import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

public class JpaConfig {
	private static EntityManagerFactory emf;

	static {
		Properties properties = new Properties();
		properties.put(AvailableSettings.DRIVER, "org.h2.Driver");
//		properties.put(AvailableSettings.URL, "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
		properties.put(AvailableSettings.URL, "jdbc:h2:file:./data/pat");
		properties.put(AvailableSettings.USER, "sa");
		properties.put(AvailableSettings.PASS, "");
		properties.put(AvailableSettings.DIALECT, "org.hibernate.dialect.H2Dialect");
//		properties.put(AvailableSettings.HBM2DDL_AUTO, "create");
		properties.put(AvailableSettings.HBM2DDL_AUTO, "update");
		properties.put(AvailableSettings.SHOW_SQL, "true");
		properties.put(AvailableSettings.HBM2DDL_IMPORT_FILES, "import.sql");

		PersistenceProvider provider = new HibernatePersistenceProvider();
		PersistenceUnitInfo persistenceUnitInfo = new PersistenceUnitInfo() {

			@Override
			public String getPersistenceUnitName() {
				return "myPersistenceUnit";
			}

			@Override
			public String getPersistenceProviderClassName() {
				return HibernatePersistenceProvider.class.getName();
			}

			@Override
			public PersistenceUnitTransactionType getTransactionType() {
				return PersistenceUnitTransactionType.RESOURCE_LOCAL;
			}

			@Override
			public DataSource getJtaDataSource() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public DataSource getNonJtaDataSource() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public List<String> getMappingFileNames() {
				return Collections.emptyList();
			}

			@Override
			public List<URL> getJarFileUrls() {
				return Collections.emptyList();
			}

			@Override
			public URL getPersistenceUnitRootUrl() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public List<String> getManagedClassNames() {
				return Arrays
						.asList(new String[] { Project.class.getName(), Account.class.getName(), Api.class.getName() });
			}

			@Override
			public boolean excludeUnlistedClasses() {
				return false;
			}

			@Override
			public SharedCacheMode getSharedCacheMode() {
				 return SharedCacheMode.UNSPECIFIED;
			}

			@Override
			public ValidationMode getValidationMode() {
				 return ValidationMode.AUTO;
			}

			@Override
			public Properties getProperties() {
				return properties;
			}

			@Override
			public String getPersistenceXMLSchemaVersion() {
				return "2.2";
			}

			@Override
			public ClassLoader getClassLoader() {
				return Thread.currentThread().getContextClassLoader();
			}

			@Override
			public void addTransformer(ClassTransformer transformer) {

			}

			@Override
			public ClassLoader getNewTempClassLoader() {
				return null;
			}
		};

		emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, properties);
	}

	public static EntityManagerFactory getEntityManagerFactory() {
		return emf;
	}
}
